<?php
class category_admin extends controller{
	public $category;
	public function index(){
		if(isset($_POST)){
			foreach ($_POST as $key=>$value)
				$data[$key]=trim($value);
			$this->model('category_models_index');
		}
		if(isset($_POST['add_category'])){
			unset($data['add_category']);
			$this->category_models_index->add_categories($data);
		}
		if(isset($_POST['Update_Category'])){
			unset($data['Update_Category']);
			$this->category_models_index->update_categories($data);
		}

		$filter='';
		
		if(isset($_POST['search_categories'])){
			$filter=$_POST['search_categories'];
		}
		
		$adminMenu= $this->load->Module('adminMenu/index');
		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		$option_category=self::wp_dropdown_cats();
		$this->load->helper('form');
		
		$tableCategoryHeader=array(0=>null,'name'=>'Name','slug'=>'url friendly','description'=>'Description','count'=>'Post');
		self::listTableCategory( 0,  0, 0,$filter);
		$data=array(
					'update_footer'=>'test',
					'admin_title'=>'Control Admin',
					'option_category'=>$option_category,
					'admin_menu'=>$adminMenu->generateAdminMenu(),
					'tableCategoryHeader'=>$tableCategoryHeader,
					'tableCategoryBody'=>$this->category
				);
		$this->view(
				array('admin-header',
						'category',
						'admin-footer'), 
				$data); // Display the page
	}
	public function delete($id){
		$this->model('category_models_index');
		$check_subCategory=$this->category_models_index->get_categories($id);
		
		if(count($check_subCategory)==0){
			$this->category_models_index->delete_categories($id);
		}else{
			foreach ($check_subCategory as $subCategory)
				$subCatName[]=$subCategory->name;
			$_SESSION['notification'][]="This Category is not empty please move/delete sub-category (".implode(', ',$subCatName).")";
		}
		$this->load->helper('url');
		redirect('category/admin');
	}

	public function edit($id){
	
		$category=$this->model('category_models_index')->get_category($id);
		$adminMenu= new adminMenu_index;
		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		$option_category=self::wp_dropdown_cats($category[0]->parent);
		$this->load->helper('form');
		
		$tableCategoryHeader=array(0=>null,'name'=>'Name','link'=>'url friendly','description'=>'Description','count'=>'Post');
		self::listTableCategory();
		$data=array(
					'update_footer'=>'test',
					'admin_title'=>'Control Admin',
					'option_category'=>$option_category,
					'admin_menu'=>$adminMenu->generateAdminMenu(),
					'category'=>$category[0]
				);
		
		$this->view(
				array('admin-header',
						'category-edit',
						'admin-footer'), 
				$data);
	}
	
	
	private function checkURL(){
		if(isset($_POST) and isset($_POST['url'])){
			if($url=trim($_POST['url'])) echo $url;
		}
	}
	public function listTableCategory( $currentcat = 0,  $parent = 0, $level = 0,$filter=''){
		$categories = $this->model('category_models_index')->get_categories( $parent,$filter );
		
		if ( is_array($categories) and !empty($categories) ) {
			foreach ( $categories as $cat ) {
				$pad = str_repeat( '&#8211; ', $level );
				$current= ( $currentcat == $cat->id )?1:0;
				
				$catRow=array("id"=>$cat->id,
										"parent"=>$cat->parent,
										"name"=>"$pad{$cat->name}",
										"short-name"=>$cat->name,
										"slug"=>$cat->slug,
										"link"=>$cat->link,
										"description"=>$cat->description,
										"order"=>$cat->order,
										"active"=>$cat->active,
										"count"=>$cat->count,
										"current"=>$current);
				
				$this->category[$cat->id]=$catRow;
				self::listTableCategory( $currentcat, $cat->id, $level +1,$filter );
			}
		}
	
	}
	public function wp_dropdown_cats( $currentcat = 0,  $parent = 0, $level = 0, $category = '' ) {
		$categories = $this->model('category_models_index')->get_categories( $parent );
		
		if ( is_array($categories) and !empty($categories) ) {
			foreach ( $categories as $cat ) {
					$pad = str_repeat( '&#8211; ', $level );
					$s= ( $currentcat == $cat->id )?" selected='selected'":'';
					$catOption=  "<option value='{$cat->id}' $s >$pad{$cat->name}</option>\n\t";
					$category .=self::wp_dropdown_cats( $currentcat, $cat->id, $level +1, $catOption );
			}
		} 
		return $category;
	}


	public function check_list_cats( $currentcat = array(),  $parent = 0, $level = 0, $category = '' ) {
		$categories = $this->model('category_models_index')->get_categories( $parent );
		$catOption='';
		if ( is_array($categories) and !empty($categories) ) {
			if($level)$catOption.=  "\n<ul class='children'>\n";
			foreach ( $categories as $cat ) {
				//$pad = str_repeat( '&#8211; ', $level );
				$s= ( in_array($cat->id, $currentcat) )?" checked":'';
				$catOption.=  "
				<li class='popular-category' id='category-{$cat->id}'>
					<label class='selectit'>";
				$sub_cat=$this->category_models_index->get_categories(  $cat->id );
				if(count($sub_cat)==0)
					$catOption.=  "<input value='{$cat->id}' type='checkbox' name='category[]' id='in-category-{$cat->id}' $s />";
				$catOption.=  " {$cat->name}</label>";
				$catOption .=self::check_list_cats( $currentcat, $cat->id, $level +1, $category );
				$catOption .=  "
				</li>";
			}
	
			if($level)$catOption.=  "\n</ul>\n";
		}
			return $catOption;
	}

	public function check_list_top_cats() {
		$categories = $this->model('category_models_index')->get_top_categories();
		$catOption='';
		if ( is_array($categories) and !empty($categories) ) {
			foreach ( $categories as $cat ) {
				$catOption.=  "
				<li class='popular-category' id='category-{$cat->id}'>
				<label class='selectit'><input value='{$cat->id}' type='checkbox' name='post_category[]' id='in-category-{$cat->id}' /> {$cat->name}</label>
				</li>";
			}
		}
			return $catOption;
	}
	public function get_link_category($id_category){
		return $this->model('category_models_index')->get_link_category($id_category);
	}
	public function get_name_category($id_category){
		return $this->model('category_models_index')->get_name_category($id_category);
	}
}